package com.cljz.makepassenger.ui.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.ui.adapter.ActivityPicPagerAdapter;
import com.cljz.makepassenger.utils.L;
import com.jakewharton.rxbinding.view.RxView;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/5/6.
 */

public class PicActivity extends BaseActivity implements ViewPager.OnPageChangeListener, ActivityPicPagerAdapter.OnItemClickListener {
    private static final String TAG = "PicActivity";
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.vp_pic)
    ViewPager vpPic;
    @BindView(R.id.ll_pic_page)
    LinearLayout llPicPage;
    private ArrayList<String> picList;
    private int type;

    private ImageView[] tips;
    private ActivityPicPagerAdapter activityPicPagerAdapter;

    @Override
    public void initData(Bundle savedInstanceState) {
        picList = getIntent().getExtras().getStringArrayList("pic");
        type = getIntent().getExtras().getInt("type");
        int position = getIntent().getExtras().getInt("position");

        //设置Adapter
        activityPicPagerAdapter = new ActivityPicPagerAdapter(this, picList, type, TAG);
//        MyAdapter myAdapter = new MyAdapter();
        vpPic.setAdapter(activityPicPagerAdapter);
        //设置监听，主要是设置点点的背景
        vpPic.setCurrentItem(position);
        vpPic.setOnPageChangeListener(this);
        //将点点加入到ViewGroup中
        tips = new ImageView[picList.size()];
        for (int i = 0; i < tips.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(10, 10));
            tips[i] = imageView;
            if (i == position) {
                tips[i].setBackgroundResource(R.drawable.shape_page_indicator_focused);
            } else {
                tips[i].setBackgroundResource(R.drawable.shape_page_indicator_unfocused);
            }

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            layoutParams.leftMargin = 5;
            layoutParams.rightMargin = 5;
            llPicPage.addView(imageView, layoutParams);


        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop(PicActivity.this);
                    }
                });
//        activityPicPagerAdapter.setOnItemClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_pic;
    }

    @Override
    public Object newP() {
        return null;
    }

    @Override
    protected void setScreen() {
        super.setScreen();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.flags = (WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setAttributes(lp);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < tips.length; i++) {
            if (i == position) {
                tips[i].setBackgroundResource(R.drawable.shape_page_indicator_focused);
            } else {
                tips[i].setBackgroundResource(R.drawable.shape_page_indicator_unfocused);
            }
        }
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onItemClick(int position) {
        L.e("onItemClick");
    }

    @Override
    public void onItemLongClick(int position) {
        // TODO: 2017/6/15  长按保存图片
    }

    public class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return picList == null ? 0 : picList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);

        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(PicActivity.this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            if (type == InvitationLetterActivity.EDITOR) {
                Picasso.with(App.getInstance())
                        .load(new File(picList.get(position)))
                        .error(R.mipmap.activity_loading)
                        .placeholder(R.mipmap.activity_loading)
                        .into(imageView);
            } else if (type == InvitationLetterActivity.ACTIVITY) {
                Picasso.with(App.getInstance())
                        .load(picList.get(position))
                        .error(R.mipmap.activity_loading)
                        .placeholder(R.mipmap.activity_loading)
                        .into(imageView);
            }
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    L.e("onClick");
                }
            });
            imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    L.e("onLongClick");
                    return false;
                }
            });
            container.addView(imageView, 0);
            return imageView;
        }
    }
}
